package org.yzh.rbac.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.sample.component.mybatis.Page;
import org.sample.model.APIResult;
import org.sample.model.PageInfo;
import org.sample.model.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yzh.rbac.model.entity.PermisDO;
import org.yzh.rbac.service.PermisService;

@ApiResponses(value = {
        @ApiResponse(code = 400, message = "请求参数有误", response = APIResult.class),
        @ApiResponse(code = 500, message = "服务器异常", response = APIResult.class)}
)
@Api(description = "权限API")
@RequestMapping("permis")
@RestController
public class PermisController {

    @Autowired
    private PermisService permisService;

    @ApiOperation(value = "列表")
    @GetMapping
    public Pagination<PermisDO> list(@RequestParam(required = false) Integer pid,
                                     PageInfo pageInfo) {
        PermisDO query = new PermisDO();
        query.setPid(pid);

        Pagination result = Page.start(() -> permisService.find(query), pageInfo);
        return result;
    }

    @ApiOperation(value = "详情")
    @GetMapping("{id}")
    public PermisDO list(@PathVariable("id") Integer id) {
        PermisDO result = permisService.get(id);
        return result;
    }

    @ApiOperation(value = "新增")
    @PostMapping
    public APIResult<Boolean> add(@RequestBody PermisDO entity) {
        int row = permisService.add(entity);
        return new APIResult(row > 0);
    }

    @ApiOperation(value = "保存")
    @PutMapping("{id}")
    public APIResult<Boolean> update(@PathVariable Integer id, @RequestBody PermisDO entity) {
        entity.setId(id);
        int row = permisService.update(entity);
        return new APIResult(row > 0);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("{id}")
    public APIResult<Boolean> delete(@PathVariable Integer id) {
        int row = permisService.delete(id);
        return new APIResult(row > 0);
    }
}